/**
 * 
 */
package com.googlecode.pcapj;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.bridj.Pointer;

import com.googlecode.pcapj.bridj.sockaddr;
import com.googlecode.pcapj.bridj.sockaddr_in;

/**
 * 
 */
class IPv4AddressImpl extends SocketAddressImpl implements IPv4Address{
	private int port;
	/**
	 * @param sockaddr
	 */
	public IPv4AddressImpl(Pointer<sockaddr> sockaddr) {
		super(sockaddr);
		if( getSocketFamily() != SocketFamily.AF_INET ){
			throw new IllegalArgumentException("unexpected socket family : " + getSocketFamily());
		}
		sockaddr_in sockaddr_in = sockaddr.as(sockaddr_in.class).get();
		port = sockaddr_in.sin_port() & 0xffff;
		try {
			setInetAddress(InetAddress.getByAddress(sockaddr_in.sin_addr().s_addr().getBytes()));
		} catch (UnknownHostException e) {
			throw new PcapJRuntimeException(e);
		}
		
	}
	/* (non-Javadoc)
	 * @see com.googlecode.pcapj.IPv4Address#getPortNumber()
	 */
	public int getPortNumber() {
		return port;
	}
	@Override
	public String toString() {
		return getClass().getSimpleName() + "[socketFamily=" + getSocketFamily()
				+ ", portNumber=" + getPortNumber()
				+ ", inetAddress=" + getInetAddress() + "]";
	}
}
